﻿-- =============================================
-- Author:		zengfei
-- Create date: 03m/15/2012
-- Description:	更新送货单主表
-- =============================================
CREATE PROCEDURE [dbo].[Usp_ModifyShipFormMaster]
	@p_Action	char(1),
	@P_ShipFormNo	varchar(30), 
	@p_ReferenceNo	varchar(30),
	@p_CustomerName	varchar(200),
	@p_Department	varchar(20),
	@p_Warehouse	varchar(60), 
	@p_ShipDate	datetime,
	@p_EntryDate	datetime, 
	@p_Author	varchar(20),
	@p_Approver	varchar(20),
	@p_Employee	varchar(20),
	@p_Remark	varchar(120),
	@p_RecordID	int out,
	@p_UserID	varchar(60),
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
begin transaction

if	@p_Action = 'A'
begin
	insert into ShipFormMaster (ShipFormNo, ReferenceNo, CustomerName, Department, Warehouse, ShipDate, EntryDate, Author, Approver, Employee, Remark)
		values (@p_ShipFormNo, @p_ReferenceNo, @p_CustomerName, @p_Department, @p_Warehouse, @p_ShipDate, @p_EntryDate, @p_Author, @p_Approver, @p_Employee, @p_Remark)
		
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY
end
else if	@p_Action = 'C'
begin
	update ShipFormMaster set CustomerName = @p_CustomerName,
								Department = @p_Department,
								Warehouse = @p_Warehouse,
								ShipDate = @p_ShipDate,
								EntryDate = @p_EntryDate,
								Author = @p_Author,
								Approver = @p_Approver,
								Employee = @p_Employee,
								Remark = @p_Remark 
							where RecordID = @p_RecordID 
							
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end
else if	@p_Action = 'D'
begin
	delete from ShipFormMaster where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end

insert into ShipFormMaster_log (ShipFormNo, ReferenceNo, CustomerName, Department, Warehouse, ShipDate, EntryDate, Author, Approver, Employee, Remark, RecordID, UserID, ModifyTime, [Action])
	values (@p_ShipFormNo, @p_ReferenceNo, @p_CustomerName, @p_Department, @p_Warehouse, @p_ShipDate, @p_EntryDate, @p_Author, @p_Approver, @p_Employee, @p_Remark, @p_RecordID, @p_UserID, GETDATE(), @p_Action)
	
if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle

commit transaction

set @p_RFlag = 1


ErrHandle:

if	@@TRANCOUNT > 0	rollback transaction

set @p_RFlag = -1

